import config from 'config.js';
class HTTP {
  constructor() {
    this.RestUrl = config.projectUrl
  }
  // 普通请求
  request(params, conType) {
    wx.showLoading({
      title: '努力加载中...',
      icon: 'loading',
      mask: true
    })
    if (!params.method) {
      params.method = 'GET';
    }
    // var requestComplete = false;
    // if(params.method == 'get') {
    //   setTimeout(function() {
    //     if(requestComplete){
    //       wx.hideLoading()
    //     }  
    //   },config.limitLoadingTime)
    // }
    var nowDate = new Date().getTime()
    var that = this;
    var url = that.RestUrl + params.url;
    var conType = conType;
    var authentication = wx.getStorageSync('authentication');
    if (!conType) {
      conType = 'application/x-www-form-urlencoded';
    } else if (conType == "postJson") {
      conType = 'application/json';
    }
    // 是否登录
    wx.request({
      url: url,
      data: params.data,
      method: params.method,
      header: {
        'content-type': conType,
        'authentication': authentication
      },
      success: function (res) {
        wx.hideLoading();
        var authentication = res.header.authentication
        if (authentication)
          wx.setStorageSync('authentication', authentication)
        
        var code = res.data.code;
        if(code == "402"){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false,
            success: (res) => {
              setTimeout(() => {
                wx.reLaunch({
                  url: '/pages/login/login'
                })
              }, 1500)
            }
          });
        } else {
          params.success && params.success(res.data);
        }
      },
      complete: function(res) {
        // if(params.method == 'get') {
        //   requestComplete = true
        //   // console.log(res)
        //   var endDate = new Date().getTime()
        //   var time = endDate - nowDate
        //   if(time > config.limitLoadingTime){
        //     wx.hideLoading();
        //   }
        // }else {
          // wx.hideLoading();
        // }
      },
      fail: function (err) {
        wx.hideLoading();
        wx.showToast({
          title: '网络异常，请稍后重试',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false
        });
      }
    });
  }
  
  // 无loading请求
  noLoadrequest(params, conType) {
    var that = this;
    var url = that.RestUrl + params.url;
    var conType = conType;
    var authentication = wx.getStorageSync('authentication');
    if (!params.method) {
      params.method = 'GET';
    }
    if (!conType) {
      conType = 'application/x-www-form-urlencoded';
    } else if (conType == "postJson") {
      conType = 'application/json';
    }
    // 是否登录
    wx.request({
      url: url,
      data: params.data,
      method: params.method,
      header: {
        'content-type': conType,
        'authentication': authentication
      },
      success: function (res) {
        var authentication = res.header.authentication
        if (authentication)
          wx.setStorageSync('authentication', authentication)
        
        var code = res.data.code;
        if(code == "402"){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false,
            success: (res) => {
              setTimeout(() => {
                wx.reLaunch({
                  url: '/pages/login/login'
                })
              }, 1500)
            }
          });
        } else {
          params.success && params.success(res.data);
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '网络异常，请稍后重试',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false
        });
      }
    });
  }

  // 无token请求
  norequest(params, conType) {
    wx.showLoading({
      title: '努力加载中...',
      icon: 'loading',
      mask: true
    })
    var that = this;
    var url = that.RestUrl + params.url;
    var conType = conType;

    if (!params.method) {
      params.method = 'GET';
    }
    if (!conType) {
      conType = 'application/x-www-form-urlencoded';
    } else if (conType == "postJson") {
      conType = 'application/json';
    }

    wx.request({
      url: url,
      data: params.data,
      method: params.method,
      header: {
        'content-type': conType
      },
      success: function (res) {
        wx.hideLoading();
        var authentication = res.header.authentication
        if (authentication)
          wx.setStorageSync('authentication', authentication)
        params.success && params.success(res.data);
        
      },
      fail: function (err) {
        wx.hideLoading();
        wx.showToast({
          title: JSON.stringify(err),
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false
        });
      }
    });

  }

  // 下载
  downLoadFile(params) {
    wx.showLoading({
      title: '导出中...',
      icon: 'loading',
      mask: true
    })
    var url = this.RestUrl + params.url;
    var authentication = wx.getStorageSync('authentication');
    wx.downloadFile({
      url: url, //仅为示例，并非真实的资源
      header: {
        'authentication': authentication
      },
      success (res) {
        wx.hideLoading();
        var authentication = res.header.authentication
        if (authentication)
          wx.setStorageSync('authentication', authentication)
        if(res.data){
          if(res.data.code == "402"){
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              image: '',
              duration: 1500,
              mask: false,
              success: (res) => {
                setTimeout(() => {
                  wx.reLaunch({
                    url: '/pages/login/login'
                  })
                }, 1500)
              }
            });
          }

        } else {
          params.success && params.success(res);
        }
      },
      fail: function (err) {
        console.log(err)
        wx.hideLoading();
        wx.showToast({
          title: '网络异常，请稍后重试',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false
        });
      }
    })
  }
}
export {
  HTTP
}